package game

import (
	"MyWuXiaGame/server/src/csvs"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"time"
)

type TBaseData struct {
	PlayerLevel    int                  //等级
	PlayerExp      int                  //阅历(经验)
	HP			   int
	MP			   int
}

type ModPlayer struct {
	UserId         int `json:"userid" ` //唯一id
	Icon           int                  //头像   新增icon模块
	Name           string               //名字   新增banword模块
	WeaponInfo 	   int					//当前穿戴武器
	AttrInfo  	   map[int]int		    //角色属性
	CurMapId	   int					//当前地图id
	BoDie		   bool					//是否死亡

	BaseData	   *TBaseData			//基础数据

	//看不见的字段
	Prohibit int //封禁状态
	IsGM     int //GM账号标志

	player *Player
	path   string
}

func (self *ModPlayer) SetProhibit(prohibit int) {
	self.Prohibit = prohibit
}

func (self *ModPlayer) SetIsGM(isGm int) {
	self.IsGM = isGm
}

func (self *ModPlayer) IsCanEnter() bool {
	return int64(self.Prohibit) < time.Now().Unix()
}

func (self *ModPlayer) SetName(name string) {

	if GetManageBanWord().IsBanWord(name) {
		return
	}

	self.Name = name
	fmt.Println("设置成功,名字变更为:", self.Name)
}

func (self *ModPlayer) SetIcon(iconId int) {
	if !self.player.GetModIcon().IsHasIcon(iconId) {
		//通知客户端，操作非法
		fmt.Println("没有头像:", iconId)
		return
	}

	self.Icon = iconId
	fmt.Println("变更头像为:", csvs.GetItemName(iconId), self.Icon )
}

func (self *ModPlayer) WearWeapon(weapon *Weapon, player *Player) {
	weaponConfig := csvs.GetWeaponConfig(weapon.WeaponId)
	if weaponConfig == nil {
		fmt.Println("数据异常，武器配置不存在")
		return
	}

	self.WeaponInfo = weapon.KeyId

	//重算属性
	self.ReCalcAbilitys()
}

func (self *ModPlayer) TakeOffWeapon(player *Player) {
	self.WeaponInfo = 0
	//重算属性
	self.ReCalcAbilitys()
}

func (self *ModPlayer) ReCalcAbilitys() {
	//清空属性
	for k := range self.AttrInfo {
		delete(self.AttrInfo, k)
	}

	if self.AttrInfo == nil {
		self.AttrInfo = make(map[int]int)
	}

	//重算基础属性
	self.ReCalcLevelAbilitys()

	//重算武器基础属性
	self.player.GetModWeapon().ReCalcWeaponAbilitys()
}

func (self *ModPlayer) CalcAbilitys(JobAttr csvs.TGrowthLineJobAttr) {
	if (self.AttrInfo != nil) && (JobAttr.JobAttrKeyValueList != nil) {
		for _, v := range JobAttr.JobAttrKeyValueList {
			self.AttrInfo[v.AttrIdx] = self.AttrInfo[v.AttrIdx] + v.AttrValue
		}
	}
}

func (self *ModPlayer) CurLevel() int {
	return self.BaseData.PlayerLevel
}

func (self *ModPlayer) CurExp() int {
	return self.BaseData.PlayerExp
}

func (self *ModPlayer) CurHP() int {
	return self.BaseData.HP
}

func (self *ModPlayer) CurMP() int {
	return self.BaseData.MP
}

func (self *ModPlayer) GetAttr(Attrtype int) int {
	if (Attrtype >= 1) && (Attrtype <= csvs.ATTR_MAXSIZE) {
		return self.AttrInfo[Attrtype]
	}

	return 0
}

func (self *ModPlayer) MaxHP() int {
	return self.GetAttr(csvs.ATTR_MAXHP)
}

func (self *ModPlayer) MaxMP() int {
	return self.GetAttr(csvs.ATTR_MAXMP)
}

func (self *ModPlayer) ProcAddHPMP(nHp, nMp int) {
	if (nHp >= 0) && (nMp >= 0) {
		if (self.BaseData.HP + nHp) < self.MaxHP() {
			self.BaseData.HP = self.BaseData.HP + nHp
		} else {
			self.BaseData.HP = self.MaxHP()
		}
		if (self.BaseData.MP + nMp) < self.MaxMP() {
			self.BaseData.MP = self.BaseData.MP + nMp
		} else {
			self.BaseData.MP = self.MaxMP()
		}

		if self.BaseData.HP > 0 {
			self.BoDie = false
		}
	}
}

func (self *ModPlayer) ProcDecHPMP(nHp, nMp int) {
	if (nHp >= 0) && (nMp >= 0) {
		if (self.BaseData.HP - nHp) > 0 {
			self.BaseData.HP = self.BaseData.HP - nHp
		} else {
			self.BaseData.HP = 0
		}
		if (self.BaseData.MP - nMp) > 0 {
			self.BaseData.MP = self.BaseData.MP - nMp
		} else {
			self.BaseData.MP = 0
		}

		if self.BaseData.HP <= 0 {
			self.BoDie = true
		}
	}
}

func (self *ModPlayer) AddExp(exp int, player *Player) {
	self.BaseData.PlayerExp += exp
	for {
		config := csvs.GetNowLevelConfig(self.CurLevel())
		if config == nil {
			break
		}
		if config.Exp == 0 {
			break
		}
		if self.BaseData.PlayerExp >= config.Exp {
			nextconfig := csvs.GetNowLevelConfig(self.CurLevel() + 1)
			if nextconfig != nil {
				self.BaseData.PlayerLevel += 1

				//重算属性
				self.ReCalcAbilitys()

				self.BaseData.PlayerExp -= config.Exp
			} else {
				self.BaseData.PlayerExp = config.Exp
				fmt.Println("等级已修炼至极限")
				break
			}
		} else {
			break
		}
	}
	fmt.Println("当前等级:", self.BaseData.PlayerLevel, "---当前经验：", self.BaseData.PlayerExp)
}

func (self *ModPlayer) ReCalcLevelAbilitys() {
	config := csvs.GetNowLevelConfig(self.CurLevel())
	if config == nil {
		return
	}
	self.CalcAbilitys(config.JobAttr)
}

func (self *ModPlayer) SaveData() {
	content, err := json.Marshal(self)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(self.path, content, os.ModePerm)
	if err != nil {
		return
	}
}

func (self *ModPlayer) LoadData(player *Player) {

	self.player=player
	self.path=self.player.localPath+"/player.json"

	configFile, err := ioutil.ReadFile(self.path)
	if err != nil {
		fmt.Println("error")
		return
	}
	err = json.Unmarshal(configFile, &self)
	if err != nil {
		self.InitData()
		return
	}

	if self.AttrInfo == nil {
		self.AttrInfo = make(map[int]int)
	}

	if self.BaseData == nil {
		self.BaseData = new(TBaseData)
	}

	return
}

func (self *ModPlayer) InitData() {
	if self.AttrInfo == nil {
		self.AttrInfo = make(map[int]int)
	}

	if self.BaseData == nil {
		self.BaseData = new(TBaseData)
	}

	self.BaseData.PlayerLevel = 1
	self.BaseData.PlayerExp = 0
	self.BaseData.HP = 100
	self.BaseData.MP = 100
	self.Name = "小虾米"
}